using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FiltronASP.Logger;
using FiltronASP.Helpers.EnumerableExtensions;

namespace FiltronASP.Models.DAO.WIX
{
    public class WixFilterEngineCommentsDAO : Abstracts.AbstractGenericDAO<WixFilterEngineComment, System.Nullable<int>>,
        Interfaces.WIX.IWixFilterEngineCommentsDAO
    {
        protected override string GetKeyProperty()
        {
            return "filter_id";
        }

        /// <summary>
        /// Pobieranie obiektow na podstawie identyfikatora powiązania silnika-filtra.
        /// </summary>
        /// <param name="engineId">silnik-filtr</param>
        /// <returns>rekordy o zadanym silniku lub null, gdy nie ma</returns>
        public IQueryable<WixFilterEngineComment> FindByFilterEngineId(int filterEngineId)
        {
            if (filterEngineId.Equals(0))
            {
                return null;
            }   //if
            try
            {
                IQueryable<WixFilterEngineComment> filters = this._cmsEntitiesDataContext.WixFilterEngineComments.Where(
                            fien => fien.filter_engine_id.Equals(filterEngineId)
                        );
                if (!(filters.Any()))
                {
                    return null;
                }   //if
                return filters;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }

        /// <summary>
        /// Pobieranie obiektow na podstawie identyfikatora powiązania silnika-filtra, 
        /// ograniczone tylko do tych, które mają wyłącznie opis (bez daty).
        /// </summary>
        /// <param name="engineId">filtr-silnik</param>
        /// <returns>rekordy o zadanym silniku lub null, gdy nie ma</returns>
        public IQueryable<WixFilterEngineComment> FindByFilterEngineIdOnlyDescription(int filterEngineId)
        {
            if (filterEngineId.Equals(0))
            {
                return null;
            }   //if
            try
            {
                IQueryable<WixFilterEngineComment> filters = this._cmsEntitiesDataContext.WixFilterEngineComments.Where(
                            fien => (fien.filter_engine_id.Equals(filterEngineId)) && (fien.filter_additional_info.Length > 2) &&
                                (fien.applicable_year == null)
                        );
                if (!(filters.Any()))
                {
                    return null;
                }   //if
                return filters;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }

        public int[] FindByFilterEngineIdOnlyDate(int filterEngineId)
        {
            if (filterEngineId.Equals(0))
            {
                return null;
            }   //if
            try
            {
                CMSEntitiesDataContext context = this._cmsEntitiesDataContext;

                var filters = 
                    from f in context.WixFilterEngineComments
                    where 
                        f.filter_engine_id.Equals(filterEngineId) && 
                        f.applicable_year != null && 
                        f.applicable_year > 0  &&
                        f.filter_additional_info.Length < 2
                    group f by 0 into fi
                    select fi;
                if (filters == null)
                {
                    return null;
                }   //if
                if (filters.Count() == 0)
                {
                    return null;
                }   //if
                var filter = (from fi in filters
                    select new { Min = fi.Min(fii => fii.applicable_year), Max = fi.Max(fii => fii.applicable_year) }
                    ).SingleOrDefault();
                if (filter == null)
                {
                    return null;
                }   //if
                int[] toReturn = new int[2];
                toReturn[0] = (int)filter.Min;
                toReturn[1] = (int)filter.Max;

                return toReturn;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }


        public Dictionary<string,int[]> FindByFilterEngineIdDescAndYear(int filterEngineId)
        {
            if (filterEngineId.Equals(0))
            {
                return null;
            }   //if
            try
            {
                CMSEntitiesDataContext context = this._cmsEntitiesDataContext;

                var filters =
                    from f in context.WixFilterEngineComments
                    where
                        f.filter_engine_id.Equals(filterEngineId) &&
                        f.applicable_year != null &&
                        f.applicable_year > 0 &&
                        f.filter_additional_info.Length > 2
                    select f;
                
                if (!(filters.Any()))
                {
                    return null;
                }   //if

                Dictionary<string, int[]> toReturn = new Dictionary<string, int[]>();
                foreach (WixFilterEngineComment filter in filters)
                {
                    if (!toReturn.ContainsKey(filter.filter_additional_info))
                    {
                        toReturn.Add(filter.filter_additional_info, new int[2]);
                        toReturn[filter.filter_additional_info][0] = (int)filter.applicable_year;
                        toReturn[filter.filter_additional_info][1] = (int)filter.applicable_year;
                    }
                    else
                    {
                        if (((int)filter.applicable_year) > toReturn[filter.filter_additional_info][1])
                            toReturn[filter.filter_additional_info][1] = (int)filter.applicable_year;
                        else if (((int)filter.applicable_year) < toReturn[filter.filter_additional_info][0])
                            toReturn[filter.filter_additional_info][0] = (int)filter.applicable_year;
                    }

                }

                return toReturn;

            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }

        /// <summary>
        /// Pobieranie obiektow na podstawie identyfikatora powiązania silnika-filtra, 
        /// ograniczone tylko do tych, które mają wyłącznie opis (bez daty).
        /// </summary>
        /// <param name="engineId">filtr-silnik</param>
        /// <returns>rekordy o zadanym silniku lub null, gdy nie ma</returns>
        public IQueryable<WixFilterEngineComment> FindByFilterEngineIdAndDate(int filterEngineId, string infoYears)
        {
            if (filterEngineId.Equals(0))
            {
                return null;
            }   //if
            try
            {
                string[] years = infoYears.Split(';');
                int year_from = Convert.ToInt32(years[0]);
                int year_to = Convert.ToInt32(years[1]);
                IQueryable<WixFilterEngineComment> filters = this._cmsEntitiesDataContext.WixFilterEngineComments.Where(
                            fien => (fien.filter_engine_id.Equals(filterEngineId)) && (fien.applicable_year >= year_from) &&
                                 (fien.applicable_year <= year_to) && (fien.filter_additional_info.Length < 2)
                        );
                if (!(filters.Any()))
                {
                    return null;
                }   //if
                return filters;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }
    }
}
